package jwcb.javapjframework.ms_template.service.service;

import com.fasterxml.jackson.core.type.TypeReference;
import jwcb.javapjframework.common.redis.RedisService.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Service
public class RedisSetTestService {

    @Autowired
    private RedisService redisService;

    private static final String TEST_KEY = "test:set:operations";

    public String testRedisSetOperations() {
        StringBuilder result = new StringBuilder();
        result.append("Redis Set 操作测试开始...\n\n");

        try {
            // 清理旧数据
            redisService.deleteObject(TEST_KEY); // 假设有通用的delete方法，或者使用redisTemplate直接删除
            
            // 1. 添加单个元素测试
            redisService.addMember(TEST_KEY, "Apple");
            Set<String> setAfterAdd1 = redisService.getCacheSet(TEST_KEY, new TypeReference<Set<String>>() {});
            result.append("测试1: 添加单个元素\n")
                  .append("预期: [Apple], 实际: ").append(setAfterAdd1).append("\n")
                  .append("结果: ").append(setAfterAdd1.contains("Apple") && setAfterAdd1.size() == 1 ? "成功" : "失败").append("\n\n");

            // 2. 添加多个元素测试
            redisService.addMember(TEST_KEY, "Banana", "Cherry", "Apple"); // 包含重复元素
            Set<String> setAfterAdd2 = redisService.getCacheSet(TEST_KEY, new TypeReference<Set<String>>() {});
            result.append("测试2: 添加多个元素(含重复)\n")
                  .append("预期: [Apple, Banana, Cherry], 实际: ").append(setAfterAdd2).append("\n")
                  .append("结果: ").append(setAfterAdd2.size() == 3 && 
                         setAfterAdd2.containsAll(Arrays.asList("Apple", "Banana", "Cherry")) ? "成功" : "失败").append("\n\n");

            // 3. 删除单个元素测试
            redisService.deleteMember(TEST_KEY, "Apple");
            Set<String> setAfterRemove1 = redisService.getCacheSet(TEST_KEY, new TypeReference<Set<String>>() {});
            result.append("测试3: 删除单个元素\n")
                  .append("预期: [Banana, Cherry], 实际: ").append(setAfterRemove1).append("\n")
                  .append("结果: ").append(setAfterRemove1.size() == 2 && 
                         setAfterRemove1.containsAll(Arrays.asList("Banana", "Cherry")) ? "成功" : "失败").append("\n\n");

            // 4. 删除多个元素测试
            redisService.deleteMember(TEST_KEY, "Banana", "Cherry", "Date"); // 包含不存在的元素
            Set<String> setAfterRemove2 = redisService.getCacheSet(TEST_KEY, new TypeReference<Set<String>>() {});
            result.append("测试4: 删除多个元素(含不存在元素)\n")
                  .append("预期: [], 实际: ").append(setAfterRemove2).append("\n")
                  .append("结果: ").append(setAfterRemove2.isEmpty() ? "成功" : "失败").append("\n\n");

            // 5. 复杂类型测试
            Set<User> userSet = new HashSet<>();
            userSet.add(new User(1, "Alice"));
            userSet.add(new User(2, "Bob"));
            redisService.addMember(TEST_KEY, userSet.toArray());
            
            Set<User> resultSet = redisService.getCacheSet(TEST_KEY, new TypeReference<Set<User>>() {});
            result.append("测试5: 复杂类型支持\n")
                  .append("预期: [User{id=1, name='Alice'}, User{id=2, name='Bob'}], 实际: ").append(resultSet).append("\n")
                  .append("结果: ").append(resultSet.size() == 2 && 
                         resultSet.stream().anyMatch(u -> u.getId() == 1 && "Alice".equals(u.getName())) &&
                         resultSet.stream().anyMatch(u -> u.getId() == 2 && "Bob".equals(u.getName())) ? "成功" : "失败").append("\n\n");

            // 6. 空Set处理测试
            redisService.deleteMember(TEST_KEY, userSet.toArray());
            Set<User> emptySet = redisService.getCacheSet(TEST_KEY, new TypeReference<Set<User>>() {});
            result.append("测试6: 空Set处理\n")
                  .append("预期: [], 实际: ").append(emptySet).append("\n")
                  .append("结果: ").append(emptySet.isEmpty() ? "成功" : "失败").append("\n\n");

            result.append("✅ 所有Set操作测试完成!");

        } catch (Exception e) {
            result.append("\n❌ 测试异常: ").append(e.getMessage());
            e.printStackTrace();
        } finally {
            // 清理测试数据
            redisService.deleteObject(TEST_KEY);
        }

        return result.toString();
    }
    
    // 测试用复杂类型
    static class User {
        private int id;
        private String name;
        
        public User() {}
        
        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }

        // getters and setters
        public int getId() { return id; }
        public void setId(int id) { this.id = id; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }

        @Override
        public String toString() {
            return "User{" + "id=" + id + ", name='" + name + '\'' + '}';
        }
    }
}